
cc.Class({
    extends: cc.Component,

    properties: {
        drawLineNode:cc.Graphics,
        time:0,
        moveSender:4
    },

    setInfo(tp,tnode){
        this.tp = tp;
        this.tnode = tnode;
        var disX = Math.abs(this.tp.x-this.tnode.x);
        var disY = Math.abs(this.tp.y-this.tnode.y);
        this.control = -1;
        if(disX<disY){
            //横向偏移
            if(this.tnode.x<this.tp.x){
                this.control = 1;
            }else{
                this.control = 2;
            }
        }else{
            if(this.tnode.y<this.tp.y){
                this.control = 3;
               
            }else{
                this.control = 4;
            }
        }
        this.drawAction();
    },

    drawAction(){
        console.log(this.time)
        this.drawLineNode.clear();
        this.drawLineNode.moveTo(this.tp.x,this.tp.y)
        var conP = cc.v2(0,0);
        var conP1 = cc.v2(0,0);
        switch(this.control){
            case 1:
                conP = cc.v2(this.tp.x-this.time,this.tp.y)
                conP1 = cc.v2(this.tp.x-this.time*2,this.tp.y)
                this.tnode.x-=this.moveSender*2;
            break;
            case 2:
                conP = cc.v2(this.tp.x+this.time,this.tp.y)
                conP1 = cc.v2(this.tp.x+this.time*2,this.tp.y)
                this.tnode.x+=this.moveSender*2;
            break;
            case 3:
                conP = cc.v2(this.tp.x,this.tp.y-this.time)
                conP1 = cc.v2(this.tp.x,this.tp.y-this.time*2)
                this.tnode.y-=this.moveSender*2;
            break;
            case 4:
                conP = cc.v2(this.tp.x,this.tp.y+this.time)
                conP1 = cc.v2(this.tp.x,this.tp.y+this.time*2)
                this.tnode.y+=this.moveSender*2;
            break;
        }
        this.drawLineNode.bezierCurveTo(conP.x,conP.y,conP1.x,conP1.y, this.tnode.x,this.tnode.y);
        this.drawLineNode.stroke();
    },

    update(dt){
        this.time+=this.moveSender;
        if(this.tnode && this.tnode.name!=""){
            var mOm = this.tnode.position.sub(this.tp)
            var tmpDis = Math.abs(mOm.mag());
            if(tmpDis>500){
                this.tnode.getComponent(this.tnode.name+"").clearState_muing();
                this.closeMe();
            }else{
               
                this.drawAction()
            }
        }else{
            this.closeMe();
        }
    },

    closeMe(){
        this.drawLineNode.clear();
        this.node.destroy();
    },

});
